﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

using FSViet.Survey.Helpers;

namespace FSViet.Survey.Administration.Answer
{
	public partial class Details : DetailsPageBase
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			CheckQuestion();

			if (!IsPostBack)
			{
				if (ItemId != Guid.Empty)
					LoadData(ItemId);
			}
		}

		void LoadData(Guid id)
		{
			TitleLabel.Text = "Update item: " + GlobalApplication.GetGuidS(id);
			try
			{
				DataHelper dataH = new DataHelper();

				var data = dataH.Load("SELECT * FROM Answers WHERE Id='" + GlobalApplication.GetGuidS(id) + "'", 0, 1);
				FillData(data.Rows[0]);
			}
			catch (Exception ex)
			{
				msg.Text = ex.Message;
#if DEBUG
				msg.Text = ex.ToString();
#endif
			}
		}

		void FillData(DataRow data)
		{
			NameInput.Text = data["Name"].ToString().Trim();
			IsCorrectCheck.Checked = (bool)data["IsCorrect"]; ;
			IsAllowCommentCheck.Checked = (bool)data["IsAllowComment"]; ;
		}

		protected void SaveButton_Click(object sender, EventArgs e)
		{
			if (!IsValidInput()) return;

			try
			{
				DataHelper dataH = new DataHelper();
				if (ItemId == Guid.Empty)
				{
					dataH.Execute("INSERT INTO Answers(Id, Name, IsCorrect, IsAllowComment, SiteId, CampaignId, QuestionId) VALUES(@Id, @Name, @IsCorrect, @IsAllowComment, @SiteId, @CampaignId, @QuestionId)",
						dataH.CreateParam("Id", SqlDbType.Char, GlobalApplication.GuidS),
						dataH.CreateParam("Name", SqlDbType.NVarChar, NameInput.Text.Trim()),
						dataH.CreateParam("IsCorrect", SqlDbType.Bit, IsCorrectCheck.Checked),
						dataH.CreateParam("IsAllowComment", SqlDbType.Bit, IsAllowCommentCheck.Checked),
						dataH.CreateParam("SiteId", SqlDbType.Char, GlobalApplication.GetGuidS(SiteId)),
						dataH.CreateParam("CampaignId", SqlDbType.Char, GlobalApplication.GetGuidS(CampaignId)),
						dataH.CreateParam("QuestionId", SqlDbType.Char, GlobalApplication.GetGuidS(QuestionId)));
				}

				else
				{
					dataH.Execute("UPDATE Answers SET Name=@Name, IsCorrect=@IsCorrect, IsAllowComment=@IsAllowComment WHERE Id=@Id",
						dataH.CreateParam("Id", SqlDbType.Char, GlobalApplication.GetGuidS(ItemId)),
						dataH.CreateParam("Name", SqlDbType.NVarChar, NameInput.Text.Trim()),
						dataH.CreateParam("IsCorrect", SqlDbType.Bit, IsCorrectCheck.Checked),
						dataH.CreateParam("IsAllowComment", SqlDbType.Bit, IsAllowCommentCheck.Checked));
				}

				//return to list
				Response.Redirect("List.aspx");
			}
			catch (Exception ex)
			{
				msg.Text = ex.Message;
#if DEBUG
				msg.Text = ex.ToString();
#endif
			}
		}

		bool IsValidInput()
		{
			bool result = true;

			if (NameInput.Text.Trim().Length < 1)
			{
				result = false;
				msg.Text = "Please enter name";
			}

			return result;
		}

		protected void DeleteButton_Click(object sender, EventArgs e)
		{
			try
			{
				var dataH = new DataHelper();
				if (ItemId != Guid.Empty)
				{
					dataH.Execute("DELETE FROM Answers WHERE Id='" + GlobalApplication.GetGuidS(ItemId) + "'");
				}
				Response.Redirect("List.aspx");
			}
			catch (Exception ex)
			{
				msg.Text = ex.Message;
#if DEBUG
				msg.Text = ex.ToString();
#endif
			}
		}
	}
}